/**
 * <pre>
 * Copyright:		Copyright(C) 2011-2012, hxhk.com
 * Filename:		com.ygsoft.util.dwz.Pager.java
 * Class:			Pager
 * Date:			2012-6-11
 * Author:			<a href="mailto:hxhk@gmail.com">hxhk</a>
 * Version          1.1.0
 * Description:		
 *
 * </pre>
 **/

package com.hxhk.util.dwz;

/**
 * 
 * @author <a href="mailto:hxhk@gmail.com">hxhk</a> Version 1.1.0
 * @since 2012-6-11 上午10:28:30
 */

public class PageInfo {
	public final static String ORDER_DIRECTION_ASC = "ASC";
	public final static String ORDER_DIRECTION_DESC = "DESC";

	/**
	 * 默认每页记录数
	 */
	private static final int DEFAULT_PAGE_SIZE = 15;

	/**
	 * 原始页码
	 */
	private int plainPageNum = 1;

	/**
	 * 当前页码
	 */
	private int pageNum = 1;
	private int numPerPage = DEFAULT_PAGE_SIZE;
	private String orderField = "";
	private String orderDirection = ORDER_DIRECTION_DESC;

	/**
	 * 总页数
	 */
	private int totalPage = 1;

	/**
	 * 前一页
	 */
	private int prePage = 1;

	/**
	 * 下一页
	 */
	private int nextPage = 1;

	// 起始行数
	private int startIndex;
	// 结束行数
	private int lastIndex;

	/**
	 * 总记录数
	 */
	private int totalCount = 0;

	public int getStartIndex() {
		setStartIndex();
		return startIndex;
	}

	public void setStartIndex() {
		this.startIndex = (pageNum - 1) * numPerPage;
	}
	
    public int getLastIndex() {
        setLastIndex();
        return lastIndex;
    }

    // 计算结束时候的索引

    public void setLastIndex() {
        if( totalCount < numPerPage){
            this.lastIndex = totalCount;
        }else if((totalCount % numPerPage == 0) 
                || (totalCount % numPerPage != 0 && pageNum < totalPage)){
            this.lastIndex = pageNum * numPerPage;
        }else if(totalCount % numPerPage != 0 && pageNum == totalPage){
            // 最后一页
            this.lastIndex = totalCount ;
        }

    }

	/**
	 * 返回 pageNum 的值
	 * 
	 * @return pageNum
	 */
	public int getPageNum() {
		if (pageNum > totalPage) {
			pageNum = totalPage;
		}
		return pageNum;
	}

	/**
	 * 设置 pageNum 的值
	 * 
	 * @param pageNum
	 */
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum > 0 ? pageNum : 1;
		this.plainPageNum = this.pageNum;
	}

	/**
	 * 返回 numPerPage 的值
	 * 
	 * @return numPerPage
	 */
	public int getNumPerPage() {
		return numPerPage;
	}

	/**
	 * 设置 numPerPage 的值
	 * 
	 * @param numPerPage
	 */
	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage > 0 ? numPerPage : 10;
	}

	/**
	 * 返回 orderField 的值
	 * 
	 * @return orderField
	 */
	public String getOrderField() {
		return orderField;
	}

	/**
	 * 设置 orderField 的值
	 * 
	 * @param orderField
	 */
	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	/**
	 * 返回 orderDirection 的值
	 * 
	 * @return orderDirection
	 */
	public String getOrderDirection() {
		return orderDirection;
	}

	/**
	 * 设置 orderDirection 的值
	 * 
	 * @param orderDirection
	 */
	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	/**
	 * 返回 totalPage 的值
	 * 
	 * @return totalPage
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * 设置 totalPage 的值
	 * 
	 * @param totalPage
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	/**
	 * 返回 prePage 的值
	 * 
	 * @return prePage
	 */
	public int getPrePage() {
		prePage = pageNum - 1;
		if (prePage < 1) {
			prePage = 1;
		}
		return prePage;
	}

	/**
	 * 设置 prePage 的值
	 * 
	 * @param prePage
	 */
	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	/**
	 * 返回 nextPage 的值
	 * 
	 * @return nextPage
	 */
	public int getNextPage() {
		nextPage = pageNum + 1;
		if (nextPage > totalPage) {
			nextPage = totalPage;
		}

		return nextPage;
	}

	/**
	 * 设置 nextPage 的值
	 * 
	 * @param nextPage
	 */
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	/**
	 * 返回 totalCount 的值
	 * 
	 * @return totalCount
	 */
	public long getTotalCount() {
		return totalCount;
	}

	/**
	 * 设置 totalCount 的值
	 * 
	 * @param totalCount
	 */
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
		totalPage = (totalCount - 1) / this.numPerPage + 1;
	}

	/**
	 * 返回 plainPageNum 的值
	 * 
	 * @return plainPageNum
	 */
	public int getPlainPageNum() {
		return plainPageNum;
	}

	/**
	 * 设置 plainPageNum 的值
	 * 
	 * @param plainPageNum
	 */
	public void setPlainPageNum(int plainPageNum) {
		this.plainPageNum = plainPageNum;
	}

}
